package org.example.ui.administrator;


import org.example.manage.UserManage;
import org.example.model.User;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ManageUserItem extends JPanel {
    private JLabel[] labels = new JLabel[5];
    private JButton[] buttons = new JButton[3];
    private SpringLayout layout = new SpringLayout();
    private JCheckBox box;
    private JComboBox<String> jComboBox;
    private User user;
    private String[] identity = {"普通用户", "店员", "管理员"};
    public boolean aBoolean = true;
    public static boolean isSave = true;
    public ManageUserItem(User user, int n) {
        setLayout(layout);
        this.user = user;
        Font font = new Font("", Font.BOLD, 15);
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        String[] userStr = {String.valueOf(n), "data/" + user.avatarPath, user.name, user.mailbox, user.condition};
        JTable table = new JTable();
        TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
        box = (JCheckBox) renderer;
        Image image2 = new ImageIcon("data/background/bag_icon.jpg").getImage().
                getScaledInstance(10, 10, Image.SCALE_DEFAULT);
        box.setIcon(new ImageIcon(image2));
        box.setSelectedIcon(new ImageIcon(image2));
        add(box);
        setIdentity();
        add(jComboBox);
        layout.putConstraint(SpringLayout.WEST, box, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, box, 40, SpringLayout.NORTH, this);
        for (int i = 0; i < 5; i++) {
            if (i == 1) {
                Image image = new ImageIcon(userStr[i]).getImage().
                        getScaledInstance(75, 75, Image.SCALE_DEFAULT);
                labels[i] = new JLabel("", JLabel.CENTER);
                add(labels[i]);
                labels[i].setIcon(new ImageIcon(image));
                labels[i].setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
                layout.putConstraint(SpringLayout.WEST, labels[i], 0, SpringLayout.EAST, labels[i - 1]);
            } else {
                labels[i] = new JLabel(userStr[i], JLabel.CENTER);
                labels[i].setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
                labels[i].setFont(font);
                if (i == 0) {
                    layout.putConstraint(SpringLayout.WEST, labels[i], 0, SpringLayout.EAST, box);
                } else {
                    layout.putConstraint(SpringLayout.WEST, labels[i], 0, SpringLayout.EAST, labels[i - 1]);
                }
                if (i == 4) {
                    layout.putConstraint(SpringLayout.WEST, jComboBox, 0, SpringLayout.EAST, labels[i - 1]);
                    layout.putConstraint(SpringLayout.NORTH, jComboBox, 30, SpringLayout.NORTH, this);
                    layout.putConstraint(SpringLayout.SOUTH, jComboBox, 70, SpringLayout.NORTH, this);
                    layout.putConstraint(SpringLayout.WEST, labels[i], 0, SpringLayout.EAST, jComboBox);
                }
                add(labels[i]);
            }

            layout.putConstraint(SpringLayout.NORTH, labels[i], 0, SpringLayout.NORTH, this);
            layout.putConstraint(SpringLayout.SOUTH, labels[i], 0, SpringLayout.SOUTH, this);
        }
        layout.putConstraint(SpringLayout.EAST, labels[0], 100, SpringLayout.EAST, box);
        layout.putConstraint(SpringLayout.EAST, labels[1], 100, SpringLayout.EAST, labels[0]);
        layout.putConstraint(SpringLayout.EAST, labels[2], 190, SpringLayout.EAST, labels[1]);
        layout.putConstraint(SpringLayout.EAST, labels[3], 190, SpringLayout.EAST, labels[2]);
        layout.putConstraint(SpringLayout.EAST, jComboBox, 100, SpringLayout.EAST, labels[3]);
        layout.putConstraint(SpringLayout.EAST, labels[4], 100, SpringLayout.EAST, jComboBox);
        String[] operate = {"修改", "拉黑", "保存"};
        for (int i = 0; i < 3; i++) {
            buttons[i] = new JButton(operate[i]);
            add(buttons[i]);
            if (i == 0) {
                layout.putConstraint(SpringLayout.WEST, buttons[i], 9, SpringLayout.EAST, labels[4]);

            } else {
                layout.putConstraint(SpringLayout.WEST, buttons[i], 5, SpringLayout.EAST, buttons[i - 1]);
            }
            layout.putConstraint(SpringLayout.NORTH, buttons[i], 35, SpringLayout.NORTH, this);
        }
        setAllListener();
        setVisible(true);
    }

    private void setAllListener() {
        buttons[0].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isSave) {
                    setBackground(Color.CYAN);
                    aBoolean = false;
                    isSave = false;
                }
                else {
                    JOptionPane.showMessageDialog(ManageUserItem.this, "你编辑的内容未保存，请保存");
                }
            }
        });
        buttons[1].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (!aBoolean&&!isSave) {
                    if (labels[4].getText().equals("正常")) {
                        labels[4].setText("被拉黑");
                        buttons[1].setText("恢复");
                    } else {
                        labels[4].setText("正常");
                        buttons[1].setText("拉黑");
                    }
                }
                else {
                    if (!isSave)
                        JOptionPane.showMessageDialog(ManageUserItem.this, "你编辑的内容未保存，请保存");
                }
            }
        });
        buttons[2].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (!aBoolean) {
                    UserManage manage = new UserManage();
                    manage.updateUser(String.format("identity='%s'", jComboBox.getItemAt(jComboBox.getSelectedIndex())),
                            String.format("mailbox='%s'", labels[3].getText()));
                    manage.updateUser(String.format("condition='%s'", labels[4].getText()),
                            String.format("mailbox='%s'", labels[3].getText()));
                    manage.close();
                    aBoolean = true;
                    isSave = true;
                    setBackground(Color.white);
                }
            }
        });
        jComboBox.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (aBoolean) {
                    if (user.identity.equals("普通用户")) {
                        jComboBox.setSelectedIndex(0);
                    }
                    else if (user.identity.equals("店员")) {
                        jComboBox.setSelectedIndex(1);
                    }
                    else {
                        jComboBox.setSelectedIndex(2);
                    }
                }
            }
        });
    }

    public void setIdentity() {
        jComboBox = new JComboBox<>(identity);
        if (user.identity.equals("普通用户")) {
            jComboBox.setSelectedIndex(0);
        }
        else if (user.identity.equals("店员")) {
            jComboBox.setSelectedIndex(1);
        }
        else {
            jComboBox.setSelectedIndex(2);
        }
        add(jComboBox);
    }
}

